{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tasseled Cap Transformation for atmospherical adjusted Landsat 8 imagery\n",
    "\n",
    "**Author:** René Kopeinig<br>\n",
    "**Description:** Tasseled Cap Transformation for Landsat 8 TOA imagery based on the scientfic work \"Derivation of a tasselled cap transformation based on Landsat 8 at-satellite reflectance\" by M.Baigab, L.Zhang, T.Shuai & Q.Tong (2014). The bands of the output image are the brightness index, greenness index and wetness index."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import Image\n",
    "import ee\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Loading Landsat 8 TOA image \n",
    "Location: Oaxaca, Veracruz, Tabasco and Chiapas México"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = ee.Image('LANDSAT/LC8_L1T_TOA/LC80230482016032LGN00')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create function for Tasseled Cap Transformation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Function\n",
    "def tasseled_cap_transformation(image):\n",
    "    b = image.select(\"B2\", \"B3\", \"B4\", \"B5\", \"B6\", \"B7\");\n",
    "    #Coefficients are only for Landsat 8 TOA imagery\n",
    "    brightness_coefficents= ee.Image([0.3029, 0.2786, 0.4733, 0.5599, 0.508, 0.1872])\n",
    "    greenness_coefficents= ee.Image([-0.2941, -0.243, -0.5424, 0.7276, 0.0713, -0.1608]);\n",
    "    wetness_coefficents= ee.Image([0.1511, 0.1973, 0.3283, 0.3407, -0.7117, -0.4559]);\n",
    "    fourth_coefficents= ee.Image([-0.8239, 0.0849, 0.4396, -0.058, 0.2013, -0.2773]);\n",
    "    fifth_coefficents= ee.Image([-0.3294, 0.0557, 0.1056, 0.1855, -0.4349, 0.8085]);\n",
    "    sixth_coefficents= ee.Image([0.1079, -0.9023, 0.4119, 0.0575, -0.0259, 0.0252]);\n",
    "    \n",
    "    #Calculate tasseled cap transformation\n",
    "    brightness = image.expression(\n",
    "        '(B * BRIGHTNESS)',\n",
    "        {\n",
    "            'B':b,\n",
    "            'BRIGHTNESS': brightness_coefficents\n",
    "        })\n",
    "    greenness = image.expression(\n",
    "        '(B * GREENNESS)',\n",
    "        {\n",
    "            'B':b,\n",
    "            'GREENNESS': greenness_coefficents\n",
    "        })\n",
    "    wetness = image.expression(\n",
    "        '(B * WETNESS)',\n",
    "        {\n",
    "            'B':b,\n",
    "            'WETNESS': wetness_coefficents\n",
    "        })\n",
    "    fourth = image.expression(\n",
    "        '(B * FOURTH)',\n",
    "        {\n",
    "            'B':b,\n",
    "            'FOURTH': fourth_coefficents\n",
    "        })\n",
    "    fifth = image.expression(\n",
    "        '(B * FIFTH)',\n",
    "        {\n",
    "            'B':b,\n",
    "            'FIFTH': fifth_coefficents\n",
    "        })\n",
    "    sixth = image.expression(\n",
    "        '(B * SIXTH)',\n",
    "        {\n",
    "            'B':b,\n",
    "            'SIXTH': sixth_coefficents\n",
    "        })\n",
    "    bright = brightness.reduce(ee.call(\"Reducer.sum\"));\n",
    "    green = greenness.reduce(ee.call(\"Reducer.sum\"));\n",
    "    wet = wetness.reduce(ee.call(\"Reducer.sum\"));\n",
    "    four = fourth.reduce(ee.call(\"Reducer.sum\"));\n",
    "    five = fifth.reduce(ee.call(\"Reducer.sum\"));\n",
    "    six = sixth.reduce(ee.call(\"Reducer.sum\"));\n",
    "    tasseled_cap = ee.Image(bright).addBands(green).addBands(wet).addBands(four).addBands(five).addBands(six)\n",
    "    return tasseled_cap.rename('brightness','greenness','wetness','fourth','fifth','sixth')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Derive and visualize Tasseled Cap Transformation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "tasseled_cap = tasseled_cap_transformation(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "palette = ['FFFFFF','C0C0C0','808080','000000','FF0000','800000','FFFF00','808000','00FF00','008000','00FFFF','008080','0000FF','000080','FF00FF','800080']\n",
    "palette = ','.join(palette)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Brightness Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"https://earthengine.googleapis.com/api/thumb?thumbid=e082c16e6a1418da0484f400c6547662&token=52f07167b6687c425d0d8fa89e6fa6e1\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "region = image.geometry().getInfo()['coordinates']\n",
    "brightness = tasseled_cap.getThumbUrl({'min':-1,'max':1,\n",
    "                                      'bands':['brightness'], 'palette':palette,'region':region})\n",
    "Image(url=brightness)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Greenness Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"https://earthengine.googleapis.com/api/thumb?thumbid=2a292ae8658e2e3978a8d66d8e184d59&token=8236beebc7465c1fa6fa5fc5e5494291\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "region = image.geometry().getInfo()['coordinates']\n",
    "greenness = tasseled_cap.getThumbUrl({'min':-1,'max':1,\n",
    "                                      'bands':['greenness'], 'palette':palette, 'region':region})\n",
    "Image(url=greenness)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Wetness Index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"https://earthengine.googleapis.com/api/thumb?thumbid=852d6287324e0f77375589a6bde73179&token=d40890a58b886b4408468cfc15e960b1\"/>"
      ],
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "region = image.geometry().getInfo()['coordinates']\n",
    "wetness = tasseled_cap.getThumbUrl({'min':-1,'max':1,\n",
    "                                      'bands':['wetness'], 'palette':palette, 'region':region})\n",
    "Image(url=wetness)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
